### -*- coding: utf-8 -*- #############################################
# Разработано компанией Ключевые Решения (http://keysolutions.ru/)
# Все права защищены, 2006-2007
#
# Developed by Key Solutions (http://keysolutions.ru/)
# All right reserved, 2006-2007
#######################################################################
"""GenerationView classes for the Zope 3 based diplom.browser package

$Id: geneticview.py 65 2007-12-28 20:27:50Z Arvi3D $
"""
__author__  = "Arvid"
__license__ = "ZPL"
__version__ = "$Revision: 65 $"
__date__ = "$Date: 2007-12-28 22:27:50 +0200 (Fri, 28 Dec 2007) $"

from zope.interface import Interface
from zope.app.i18n import ZopeMessageFactory as _
from zope.formlib.form import AddForm, EditForm, applyChanges, Fields, Field, haveInputWidgets, action, setUpDataWidgets
from zope.formlib.namedtemplate import NamedTemplate, NamedTemplateImplementation
from zope.app.pagetemplate import ViewPageTemplateFile
from zope.app import zapi
from zope.schema import getFieldsInOrder
from zope.schema.fieldproperty import FieldProperty
from zope.app.container.interfaces import IAdding

from diplom.interfaces import IDistributingFactory, IGenetic, IGenerated, IEthalon
from interfaces import IGeneration, IExploringForm
from diplom.data import Data

from random import random

class GenerationView(EditForm) :

    firstDistr = 'exponential'
    secondDistr = 'exponential'

    @property
    def firstFactory(self):
        return zapi.getUtility(IDistributingFactory, name = self.firstDistr, context = self.context)

    @property
    def secondFactory(self):
        return zapi.getUtility(IDistributingFactory, name = self.secondDistr, context = self.context)

    @property
    def form_fields(self):
        return Fields(IEthalon, IGenerated, *tuple([Fields(*tuple([Field(field, prefix = str(i))
                                                          for n, field in getFieldsInOrder(factory.params)
                                                          ])) \
                            for i, factory in enumerate([self.firstFactory, self.secondFactory])
                            ]))

    def getData(self):
        paramlist = IGenerated(self.context).params
        res = {}
        for i, params in enumerate(paramlist):
            for n, p in params:
                res["%i.%s" % (i, n)] = p

        return res

    def setUpWidgets(self, ignore_request=False):
        data = self.getData()
        self.widgets = setUpDataWidgets(
                            self.form_fields, self.prefix,
                            self.context, self.request,
                            data, ignore_request = ignore_request)


    @action(_("Regenerate"), condition=lambda *kv: False)
    def handle_edit_action(self, action, data):
        EditForm.handle_edit_action.success_handler(self, action, data)
